<?php
/**
* 	Bushido
 *
 * @category   Bushido
 * @package    Bushido_Datagram
 * @subpackage G
 * @copyright  Copyright (c) 2008-2009 Nathan Keyes
 * @author Nathan Keyes
 * @version    $Id: SQLDateRange.php 7 2009-11-14 22:42:08Z nkeyes $
 */
class Bushido_Datagram_SQLDateRange extends Bushido_Datagram_Abstract
{
	/**
	 * The name of this chart
	 * 
	 * @var string
	 */
	protected $chartDisplayName ='';
	protected $_seriesName;
	protected $_sql;
	protected $_dbh;
	protected $_startDate;
	protected $_endDate;
	protected $_interval;
	
	
	/**
	 * 
	 * @param $seriesName
	 * @param $sql
	 * @param $startDate
	 * @param $endDate
	 * @param $interval
	 * @param $dbh
	 * @param $configOptions
	 * @return void
	 */
	public function __construct($seriesName, $sql, $startDate, $endDate, $interval, $dbh, $configOptions = array())
	{
		$this->allowedConfigOptions = array('id', 'caption');
		$this->config = new Bushido_Configurable();
		$this->config->addAllowedConfigOptions($this->allowedConfigOptions);
		$this->config->setConfigOptions($configOptions);
		
		$this->setSeriesName($seriesName);
		$this->setSql($sql);
		$this->setDbh($dbh);
		$this->setStartDate($startDate);
		$this->setEndDate($endDate);
		$this->setInterval($interval);
		
		parent::__construct($configOptions);
	}
	
	/**
	 * 
	 * @return string The name of this chart
	 */
	public function getChartDisplayName()
	{
		return $this->chartDisplayName;
	}
	
	/**
	 * Sets the raw data for this chart
	 * @return void
	 */
	protected function _setRawData()
	{
		$dbDateFormat = 'Y-m-d G:i:s';
		$dateTimeToken = '[TIMESTAMP]';
				
		$seriesData = array();
		$currentDate = $this->getStartDate();
		
		while($currentDate <= $this->getEndDate())
		{
			$currentDateString = date_format($this->getStartDate(), $dbDateFormat);
			$currentSql = str_replace($dateTimeToken, $this->getIntervalTimeStampSql($currentDateString, $this->getInterval()), $this->getSql());
			$results = $this->getDbh()->fetchPairs($currentSql);
			$seriesData = array_merge($seriesData, $results);
			
			if($this->getInterval() != 'quarter')
			{
				date_modify($currentDate, '+1 ' . $this->getInterval());
			}
			else
			{
				date_modify($currentDate, '+3 month');
			}
		}
		
		$this->setSeries($this->getSeriesName(), $seriesData);
	}
	
	/**
	 * 
	 * @return string
	 */
	public function getSeriesName()
	{
		return $this->_seriesName;
	}
	
	/**
	 * 
	 * @param $seriesName
	 * @return void
	 */
	public function setSeriesName($seriesName)
	{
		$this->_seriesName = $seriesName;
	}
	
	
	/**
	 * 
	 * @return string
	 */
	public function getSql()
	{
		return $this->_sql;
	}
	
	
	/**
	 * 
	 * @param $sql
	 * @return void
	 */
	public function setSql($sql)
	{
		$this->_sql = $sql;
	}
	
	
	/**
	 * 
	 * @return Zend_Db_Adapter_Abstract
	 */
	public function getDbh()
	{
		return $this->_dbh;
	}
	
	
	/**
	 * 
	 * @param $dbh
	 * @return void
	 */
	public function setDbh($dbh)
	{
		$this->_dbh = $dbh;
	}
	
	/**
	 * 
	 * @return DateTime
	 */
	public function getStartDate()
	{
		return $this->_startDate;
	}
	
	
	/**
	 * 
	 * @param $startDate
	 * @return void
	 */
	public function setStartDate($startDate)
	{
		$this->_startDate = new DateTime($startDate);
	}
	
	
	/**
	 * 
	 * @return DateTime
	 */
	public function getEndDate()
	{
		return $this->_endDate;
	}
	
	
	/**
	 * 
	 * @param $endDate
	 * @return void
	 */
	public function setEndDate($endDate)
	{
		$this->_endDate = new DateTime($endDate);
	}
	
	
	/**
	 * 
	 * @return string
	 */
	public function getInterval()
	{
		return $this->_interval;
	}
	
	
	/**
	 * 
	 * @param $interval
	 * @return void
	 */
	public function setInterval($interval)
	{
		$this->_interval = $interval;
	}
	
	
	/**
	 * 
	 * @param $timestamp
	 * @param $interval
	 * @return string
	 */
	public function getIntervalTimeStampSql($timestamp, $interval)
	{
		switch($interval)
		{
			case 'year':
				return "DATE_FORMAT('$timestamp', '%Y-01-01 00:00:00')";
				break;
				
			case 'quarter':
				return "DATE_ADD(DATE_FORMAT('$timestamp', '%Y-01-01 00:00:00'), INTERVAL QUARTER('$timestamp')-1 QUARTER";
				break;
				
			case 'month':
				return "DATE_FORMAT('$timestamp', '%Y-%m-01 00:00:00')";
				break;
			
			case 'week':
				return "DATE_FORMAT(DATE_SUB('$timestamp', INTERVAL 6-(7-(DAYOFWEEK('$timestamp'))) DAY), '%Y-%m-%d 00:00:00')";
				break;
				
			case 'day':
				return "DATE_FORMAT('$timestamp', '%Y-%m-%d 00:00:00')";
				break;
			
			case 'hour':
				return "DATE_FORMAT('$timestamp', '%Y-%m-%d %H:00:00')";
				break;
				
			case 'minute':
				return "DATE_FORMAT('$timestamp', '%Y-%m-%d %H:%i:00')";
				break;
				
			default:
				return "DATE_FORMAT('$timestamp', '%Y-%m-%d %H:%i:%s')";
				break;
		}
	}
}